#include "Image.h"
#include "Class.h"
#include "Object.h"
#include "stdlib.h"
#include <signal.h>
#include <sigsegv.h>
#ifndef SIGSTKSZ
#define SIGSTKSZ 4096*4;
#endif
void overflow_handler(int emergency,stackoverflow_context_t scp){
    printf("ERROR: Stack overflow\n");
    exit(-1);
}
Object* Image::getObject(string name){
    if(worldDict.find(name)!=worldDict.end()){
        return worldDict[name];
    }else{
        return getClass(name);
    }
}
void Image::addObject(string name,Object* object){
    worldDict[name]=object;
}
Image::Image(){
    stackHandlePlace=malloc(SIGSTKSZ);
    //stackoverflow_install_handler(overflow_handler,stackHandlePlace,SIGSTKSZ);
    clsClass=NULL;
    clsObject=NULL;
    clsMethod=NULL;
    addObject("Image",this);
}
Image::~Image(){
    free(stackHandlePlace);
}
void Image::exception(string message){
    printf("ERROR: %s\n",message.c_str());
}
void* Image::alloc(size_t amount){
   return malloc(amount);
}
void* Image::realloc(void* mem,size_t newAmount){
   return realloc(mem,newAmount);
}
void Image::free(void* mem){
   free(mem);
}
void Image::addClass(string name,Class* cls){
    //printf("Adding class %s.\n",name.c_str());
   classes[name]=cls;
}
Class* Image::getClass(string name){
    if(classes.find(name)==classes.end()){
        exception("Can not find var "+name);
    }
   return classes[name];
}
Object* primitive_image_add_object(Image* image,Object* self,int argc,Object** argv){
    image->addObject(inspectString(argv[1]),argv[0]);
    return NULL;
}